/**
 * @Author ZhaoLei
 * @FileName index.js
 * @Description  hft 补充逻辑
 * @Date 2022-10-31 14:35
 * @Copyright By hftsoft
 */

export default {
  data() {
    return {
      hftIsBuy: false, // hft 课程购买状态
      serviceIsBuy: false, // 企业大学 课程购买状态
      viewStatus: 0, // 观看状态 0 未看完 1 已看完
    };
  },
  created() {
    window['mediaPrePlayNativeCallback'] = (jsonStr = '{}') => this.mediaPrePlayNativeCallback(jsonStr);
    window['mediaPlayPayDoneCallback'] = (jsonStr = '{}') => this.mediaPlayPayDoneCallback(jsonStr);
  },

  mounted() {
    this.hlsPlayer = document.getElementById('hlsMediaObj');
    this.hlsPlayer.addEventListener('play', this.handlePlayEvent);
    this.hlsPlayer.addEventListener('timeupdate', this.handlePlayListener);
    this.hlsPlayer.addEventListener('ended', () => {
      this.viewStatus = 1;
      console.log('..........');
      this.requestNativeMethods('onJsWatchEURecord')
    });
  },

  methods: {
    /**
     * 处理播放事件
     */
    handlePlayEvent() {
      console.log('play event');
      if (!this.$store.getters['memberInfo/isOpen']) {
        this.hlsPlayer.pause();
        this.hlsPlayer.currentTime = 0;
        this.$buyMemberPopup();
        return;
      }
      this.addPlayRecord();
      this.requestNativeMethods('onJsPreparePlayingEUVideo', 'mediaPrePlayNativeCallback');
    },

    /**
     * 处理播放监听
     */
    handlePlayListener() {
      let currentTime = this.hlsPlayer.currentTime;
      console.log('playing...');

      // 播放时间小于30s或者已开通，不做处理
      if (currentTime <= 30 || this.hftIsBuy) return;
      console.log('扣房豆');
      this.hlsPlayer.currentTime = 30;
      this.hlsPlayer.pause();

      // 执行扣豆
      this.requestNativeMethods('onJsConfirmPlayingEUVideo', 'mediaPlayPayDoneCallback');
    },

    /**
     *
     * @param {string} execFn 执行方法
     * @param {string | undefined} callback 回调函数
     */
    requestNativeMethods(execFn, callback= undefined) {
      if (!execFn) throw new Error('execFn or callback can not be null');
      if (!window.hasOwnProperty('zshft')) return;
      const {
        courseId, id: mediaId, courseType: type, title: mediaName,
        courseTitle: courseName, subjectTitle: subjectName
      } = this.getVideoData;
      let params = {
        subjectId: this.courseId,
        courseId,
        mediaId,
        type,
        mediaName,
        courseName,
        subjectName,
        viewStatus: this.viewStatus
      };
      if (callback) return window.zshft[execFn](JSON.stringify(params), callback);
      window.zshft[execFn](JSON.stringify(params));
    },

    /**
     * 播放前原生APP客户端回调
     * @param jsonStr code 0 已扣 1 未扣
     */
    mediaPrePlayNativeCallback(jsonStr) {
      let { code = '1', info } = JSON.parse(jsonStr);
      this.hftIsBuy = code * 1 === 0;
    },

    /**
     * 扣豆完成回调
     * @param jsonStr
     */
    mediaPlayPayDoneCallback(jsonStr) {
      let { code, info } = JSON.parse(jsonStr);
      this.hftIsBuy = code * 1 === 1;
      if (code * 1 === 1) this.hlsPlayer.play();
    }
  },

  destroyed() {
    this.hlsPlayer.removeEventListener('play', this.handlePlayEvent);
    this.hlsPlayer.removeEventListener('timeupdate', this.handlePlayListener);
  }
};
